﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Global;
using Clipper.ClipperWindow;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserReagentSet.xaml 的交互逻辑 试剂收盘设置界面
    /// </summary>
    public partial class UserReagentSet : UserControl
    {
        public UserReagentSet()
        {
            InitializeComponent();
        }
        #region
        BLL.SubstrateInfo mSubstrateBll = new BLL.SubstrateInfo();
        BLL.ReagentKit mReagentKitBll = new BLL.ReagentKit();

        #endregion
        public void DataBind()
        {
            if (DataContext == null)
            {
                ServiceProvider.ReagentKitVM.SubstrateInfo = mSubstrateBll.GetModel();
                if (ServiceProvider.ReagentKitVM.SubstrateInfo==null)
                {
                    ServiceProvider.ReagentKitVM.SubstrateInfo = new Model.SubstrateInfo()
                    {
                        capatity = 6000,
                        changeDate = DateTime.Now,
                        substrateAmount = 0,
                        substrateInfo = "1"
                    };
                    mSubstrateBll.Add(ServiceProvider.ReagentKitVM.SubstrateInfo);
                }
                ServiceProvider.ReagentKitVM.ReagentKitList = mReagentKitBll.GetModelList();
                this.DataContext = ServiceProvider.ReagentKitVM;
            }
        }
        //更换底物
        private void btnChangeSubstrate_Click(object sender, RoutedEventArgs e)
        {
            if (ServiceProvider.SelfCheckPass)
            {
                if (btnChangeSubstrate.Visibility == Visibility.Visible)//开始更换
                {
                    //txtSubstrateAmount.IsEnabled = true;
                    //slidSubstrateAmount.IsEnabled = true;
                    btnChangeSubstrate.Visibility = Visibility.Hidden;
                    btnCompleteChange.Visibility = Visibility.Visible;
                }
                else if(btnCompleteChange.Visibility==Visibility.Visible)//更换完成按钮
                {
                    ServiceProvider.ReagentKitVM.SubstrateInfo.changeDate = DateTime.Now;
                    UIHelper.SuccessOrFailTip(mSubstrateBll.Update(ServiceProvider.ReagentKitVM.SubstrateInfo), "ChangeSubstrate");
                    btnCompleteChange.Visibility = Visibility.Hidden;
                    btnChangeSubstrate.Visibility = Visibility.Visible;
                    //txtSubstrateAmount.IsEnabled = false;
                    //slidSubstrateAmount.IsEnabled = false;
                }
            }
            else
            {
                UIHelper.ShowKeyMessage("PleaseSelfCheck");
            }
        }
        //试剂设置开始
        private void btnReagentSet_Click(object sender, RoutedEventArgs e)
        {
            if (CheckSelect())
            {
                WinReagentBarcode win = new WinReagentBarcode();
                win.Tag = datReagentKit.SelectedItem;
                win.Closed += new EventHandler(win_Closed);
                win.ShowDialog();
            }
        }
        //关闭窗口刷新
        void win_Closed(object sender, EventArgs e)
        {
            ServiceProvider.ReagentKitVM.ReagentKitList = mReagentKitBll.GetModelList();
        }
        //卸载试剂1
        private void btnReagentUnload1_Click(object sender, RoutedEventArgs e)
        {
            if (CheckSelect())
            {
                if (UIHelper.ShowKeyAskMessage("IsUnLoadReagent"))
                {



                    Model.ReagentKit model = datReagentKit.SelectedItem as Model.ReagentKit;
                    mReagentKitBll.UnloadReagent(model, 1);
                    ServiceProvider.ReagentKitVM.ReagentKitList = mReagentKitBll.GetModelList();
                }
            }
        }
        //卸载试剂2
        private void btnReagentUnload2_Click(object sender, RoutedEventArgs e)
        {
            if (CheckSelect())
            {
                if (UIHelper.ShowKeyAskMessage("IsUnLoadReagent"))
                {
                    Model.ReagentKit model = datReagentKit.SelectedItem as Model.ReagentKit;
                    mReagentKitBll.UnloadReagent(model, 2);
                    ServiceProvider.ReagentKitVM.ReagentKitList = mReagentKitBll.GetModelList();
                }
            }   
        }
        /// <summary>
        /// 检查能否进行试剂安装或卸载
        /// </summary>
        /// <returns></returns>
        public bool CheckSelect()
        {
            bool res = false;
            if (ServiceProvider.SelfCheckPass)
            {
                if (datReagentKit.SelectedItem != null)
                {
                    res = true;
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoKitPositionTip");
                }
            }
            else
            {
                UIHelper.ShowKeyMessage("PleaseSelfCheck");
            }
            return res;
        }
        private void datReagentKit_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ServiceProvider.SelfCheckPass)
            {
                if (datReagentKit.SelectedItem != null)
                {
                    WinReagentBarcode win = new WinReagentBarcode();
                    win.Tag = datReagentKit.SelectedItem;
                    win.Closed += new EventHandler(win_Closed);
                    win.ShowDialog();
                }
            }
            else
            {
                UIHelper.ShowKeyMessage("PleaseSelfCheck");
            }
        }

        private void btnCompleteChange_Click(object sender, RoutedEventArgs e)
        {

        }


    }
}
